import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * @since 2019-12-03
 */
import { CPAAS_IAM } from 'utils/config';
import request from 'utils/request';
import { getCurrentOrganizationId, isTenantRoleLevel } from 'utils/utils';
var organizationId = getCurrentOrganizationId();
/**
 * 批量分配API
 * @async
 * @function distributeApi
 * @param {object} params - 分配参数
 */

export function distributeApi(_x) {
  return _distributeApi.apply(this, arguments);
}
/**
 * 给单一租户分配API
 * @async
 * @function distributeApi
 * @param {object} params - 分配参数
 */

function _distributeApi() {
  _distributeApi = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    var url;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/cpaas/v1/").concat(organizationId, "/permissions/assign") : "".concat(CPAAS_IAM, "/cpaas/v1/permissions/assign");
            return _context.abrupt("return", request(url, {
              method: 'POST',
              body: params
            }));

          case 2:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _distributeApi.apply(this, arguments);
}

export function distributeApiToTenant(_x2) {
  return _distributeApiToTenant.apply(this, arguments);
}
/**
 * 新建
 * @async
 * @function deleteBatchApi
 * @param {object} params - 删除项
 */

function _distributeApiToTenant() {
  _distributeApiToTenant = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    var tenantId, permissions, url;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            tenantId = params.tenantId, permissions = params.permissions;
            url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/cpaas/v1/").concat(organizationId, "/permissions/assign/").concat(tenantId) : "".concat(CPAAS_IAM, "/cpaas/v1/permissions/assign/").concat(tenantId);
            return _context2.abrupt("return", request(url, {
              method: 'POST',
              body: permissions
            }));

          case 3:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _distributeApiToTenant.apply(this, arguments);
}

export function deleteBatchApi(_x3) {
  return _deleteBatchApi.apply(this, arguments);
}
/**
 * 删除单一用户的API
 * @async
 * @function deleteSingleTenantAPI
 * @param {object} params - 删除项
 */

function _deleteBatchApi() {
  _deleteBatchApi = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    var url;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/cpaas/v1/").concat(organizationId, "/permissions") : "".concat(CPAAS_IAM, "/cpaas/v1/permissions");
            return _context3.abrupt("return", request(url, {
              method: 'DELETE',
              body: params
            }));

          case 2:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _deleteBatchApi.apply(this, arguments);
}

export function deleteSingleTenantAPI(_x4) {
  return _deleteSingleTenantAPI.apply(this, arguments);
}

function _deleteSingleTenantAPI() {
  _deleteSingleTenantAPI = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    var permissions, url;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            permissions = params.permissions;
            url = isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/cpaas/v1/").concat(organizationId, "/permissions/revoke") : "".concat(CPAAS_IAM, "/cpaas/v1/permissions/revoke");
            return _context4.abrupt("return", request(url, {
              method: 'DELETE',
              body: permissions
            }));

          case 3:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _deleteSingleTenantAPI.apply(this, arguments);
}

export function queryApiLabel(_x5) {
  return _queryApiLabel.apply(this, arguments);
}

function _queryApiLabel() {
  _queryApiLabel = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request(isTenantRoleLevel() ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/label-rels/API/").concat(params) : "".concat(CPAAS_IAM, "/v1/label-rels/API/").concat(params), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _queryApiLabel.apply(this, arguments);
}